<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package MediaLibrary
* @copyright 2009 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.joomlawebserver.com
* @version: 1.5.3 Basic $
*
**/



require_once ($mosConfig_absolute_path."/components/com_medialibrary/medialibrary.class.lend.php");
require_once ($mosConfig_absolute_path."/components/com_medialibrary/medialibrary.class.review.php");


/**
* Book database table class
*/
class mosMedialibrary extends mosDBTable {
	/** @var int Primary key */
	var $id=null;
	/** @var int */	
	var $catid=null;
	/** @var int */
	var $sid=null;
	/** @var string */	
	var $date=null;
	/** @var int */
	var $hits=null;
	/** @var int */
	var $ordering=null;
	/** @var int */
	var $published=null;
	/** @var boolean */
	var $checked_out=null;
	/** @var time */
	var $checked_out_time=null;
	/** @var int */
	var $archived=null;
	/** @var varchar(200) */	
	var $informationFrom=null;
	/** @var varchar(3) */	
	var $fk_lendid=null;
  	/** @var varchar(10) */  
	var $media_type = null;
	  /** @var int(11) */  
	var $media_id = null;
	/**
	* @param database - A database connector object
	*/
	function mosMedialibrary( &$db ) {
		$this->mosDBTable( '#__medialibrary', 'id', $db );
    //$this->media_type = "book" ;//default media type 
	}
	
	
	/**
	* @param string - Target search string
	* not used at the moment
	*/
//	function search( $text, $state='', $sectionPrefix='' ) {
//		$text = trim( $text );
//		/** if ($text == '') { **/
//			return array();
//		/**}
		
//		$this->_db->setQuery( "SELECT date AS created, title,"
//		. "\n	author,  '1' AS browsernav, '{$sectionPrefix}Books' AS section"
//		. "\nFROM #__medialibrary WHERE (title LIKE '%$text%' OR author LIKE '%$text%'"
//		. "\n)"
//		. "\n ORDER BY created DESC"
//		);
		
//		return $this->_db->loadObjectList(); **/
//	}
	
	function getReviews(){
		$this->_db->setQuery("SELECT id FROM #__medialibrary_review \n".
							"WHERE fk_mediaid='$this->id' ORDER BY id");
		$tmp = $this->_db->loadResultArray();		
		$retVal = array();		
		for($i = 0, $j = count($tmp); $i < $j; $i++ ){
			$help = new mosMediaLibrary_review($this->_db);
			$help->load(intval($tmp[$i]));
			$retVal[$i] = $help;
		}		
		return $retVal;	
	}
	
	function getLend(){
		$lend = null;
		if($this->fk_lendid != null && $this->fk_lendid != 0){
			$lend = & new mosMediaLibrary_lend($this->_db);
			// load the row from the db table
			$lend->load(intval($this->fk_lendid));
		}
		return $lend;
	}
	
	function getAllLends($exclusion=""){
		$this->_db->setQuery("SELECT id FROM #__medialibrary_lend \n".
							"WHERE fk_mediaid='$this->id' " . $exclusion . " ORDER BY id");
		$tmp = $this->_db->loadResultArray();		
		$retVal = array();		
		for($i = 0, $j = count($tmp); $i < $j; $i++ ){
			$help = new mosMediaLibrary_lend($this->_db);
			$help->load(intval($tmp[$i]));
			$retVal[$i] = $help;
		}		
		return $retVal;	
	}

	function getAllLendRequests($exclusion=""){
		$this->_db->setQuery("SELECT id FROM #__medialibrary_lend_request \n".
							"WHERE fk_mediaid='$this->id'" . $exclusion . " ORDER BY id");
		$tmp = $this->_db->loadResultArray();		
		$retVal = array();		
		for($i = 0, $j = count($tmp); $i < $j; $i++ ){
			$help = new mosMediaLibrary_lend_request($this->_db);
			$help->load(intval($tmp[$i]));
			$retVal[$i] = $help;
		}		
		return $retVal;	
	}


	function toXML(& $xmlDoc){
		
		//create and append name element 
		$retVal = & $xmlDoc->createElement("media"); 
		
		$media_id = & $xmlDoc->createElement("media_id");
		$media_id->appendChild($xmlDoc->createTextNode($this->media_id));
		$retVal->appendChild( $media_id);
		
		$media_type = & $xmlDoc->createElement("media_type");
		$media_type->appendChild($xmlDoc->createTextNode($this->media_type) );
		$retVal->appendChild( $media_type);
		
		$informationFrom = & $xmlDoc->createElement("informationFrom");
		$informationFrom->appendChild($xmlDoc->createTextNode($this->informationFrom));
		$retVal->appendChild( $informationFrom);

    $date = & $xmlDoc->createElement("date");
    $date->appendChild($xmlDoc->createTextNode($this->date));
    $retVal->appendChild( $date);

    $published = & $xmlDoc->createElement("published");
    $published->appendChild($xmlDoc->createTextNode($this->published));
    $retVal->appendChild( $published );

    //add category
    $this->_db->setQuery("SELECT name, title,section, id, parent_id, published FROM #__categories ".
              "WHERE id=".$this->catid . " ; " );
    $tmp = $this->_db->loadObjectList();   

    //print_r($tmp);

    $category = & $xmlDoc->createElement("category_entity");

    $category_id = & $xmlDoc->createElement("category_id");
    $category_id->appendChild($xmlDoc->createTextNode($tmp[0]->id));
    $category->appendChild( $category_id );

    $category_parent_id = & $xmlDoc->createElement("category_parent_id");
    $category_parent_id->appendChild($xmlDoc->createTextNode($tmp[0]->parent_id));
    $category->appendChild( $category_parent_id );

    $category_name = & $xmlDoc->createElement("category_name");
    $category_name->appendChild($xmlDoc->createTextNode($tmp[0]->name));
    $category->appendChild( $category_name );

    $category_title = & $xmlDoc->createElement("category_title");
    $category_title->appendChild($xmlDoc->createTextNode($tmp[0]->title));
    $category->appendChild( $category_title );

    $category_section = & $xmlDoc->createElement("category_section");
    $category_section->appendChild($xmlDoc->createTextNode($tmp[0]->section));
    $category->appendChild( $category_section );

    $category_published = & $xmlDoc->createElement("category_published");
    $category_published->appendChild($xmlDoc->createTextNode($tmp[0]->published));
    $category->appendChild( $category_published );

    $retVal->appendChild( $category);
		
    //add media type
    switch ($this->media_type)
    {
      case 'book':
        $media_class = new mosMedialibrary_book($this->_db);
        $media_class->load($this->media_id);
        $media_class_xml = $media_class->toXML($xmlDoc);
        $retVal->appendChild( $media_class_xml);
        break;
      case 'video':
        $media_class = new mosMedialibrary_video($this->_db);
        $media_class->load($this->media_id);
        $media_class_xml = $media_class->toXML($xmlDoc);
        $retVal->appendChild( $media_class_xml);
        break;
      case 'music':
        $media_class = new mosMedialibrary_music($this->_db);
        $media_class->load($this->media_id);
        $media_class_xml = $media_class->toXML($xmlDoc);
        $retVal->appendChild( $media_class_xml);
        break;
      case 'game':
        $media_class = new mosMedialibrary_game($this->_db);
        $media_class->load($this->media_id);
        $media_class_xml = $media_class->toXML($xmlDoc);
        $retVal->appendChild( $media_class_xml);
        break;
    }


/*			$lends_data = $this->getLend();
			$exclusion = "";
			
			$lends = & $xmlDoc->createElement("lends");
			$lends_data = $this->getAllLends($exclusion);;
			
			foreach($lends_data as $lend_data){
				$lends->appendChild($lend_data->toXML( $xmlDoc));
			}
			$retVal->appendChild( $lends);
		
			$lendrequests = & $xmlDoc->createElement("lendrequests");
			$lendrequests_data = $this->getAllLendRequests($exclusion);
			foreach($lendrequests_data as $lendrequest_data){
				$lendrequests->appendChild($lendrequest_data->toXML( $xmlDoc));
			}
			$retVal->appendChild( $lendrequests);
*/
			$reviews = & $xmlDoc->createElement("reviews");
			$reviews_data = $this->getReviews();
			foreach($reviews_data as $review_data){
				$reviews->appendChild($review_data->toXML( $xmlDoc));
			}
			$retVal->appendChild( $reviews);	
		
		return $retVal;
	}

	function toXML2($all){
	
		$retVal = "<book>\n";
		$retVal .= "<bookid>" . $this->bookid . "</bookid>\n";
		$retVal .= "<sid>" . $this->sid . "</sid>\n";
		$retVal .= "<isbn>" . $this->isbn . "</isbn>\n";
		$retVal .= "<date>" . $this->date . "</date>\n";
		$retVal .= "<hits>" . $this->hits . "</hits>\n";
		$retVal .= "<ordering>" . $this->ordering . "</ordering>\n";
		$retVal .= "<published>" . $this->published . "</published>\n";	
		$retVal .= "<archived>" . $this->archived . "</archived>\n";
		$retVal .= "<title>" . htmlspecialchars( $this->title) . "</title>\n";
		$retVal .= "<imageURL>" . $this->imageURL . "</imageURL>\n";
		$retVal .= "<URL>" . $this->URL . "</URL>\n";
		$retVal .= "<rating>" . $this->rating . "</rating>\n";
		$retVal .= "<authors>" . htmlspecialchars($this->authors) . "</authors>\n";
		$retVal .= "<manufacturer>" . htmlspecialchars($this->manufacturer) . "</manufacturer>\n";
		$retVal .= "<comment>" . $this->comment . "</comment>\n";
		$retVal .= "<informationFrom>" . $this->informationFrom . "</informationFrom>\n";	
		$retVal .= "<language>" . $this->language . "</language>\n";
		$retVal .= "<releaseDate>" . $this->release_date . "</releaseDate>\n";
		$retVal .= "<edition>" . $this->edition . "</edition>\n";
		$retVal .= "<ebookURL>" . $this->ebookURL . "</ebookURL>\n";
		$retVal .= "<price>" . $this->price . "</price>\n";
		
		if($all){
			$actualLend = $this->getLend();
			$exclusion = "";
			if($actualLend != null){
				$exclusion = "AND id <> " . $actualLend->id;
				$retVal .= $actualLend->toXML("actualLend");
			}
			
			$retVal .= "<lends>";
				$lends = $this->getAllLends($exclusion);
				foreach($lends as $lend){
					$retVal .= $lend->toXML();
				}
			$retVal .= "</lends>";
			
			
			$retVal .= "<lendrequests>";
				$lendrequests = $this->getAllLendRequests($exclusion);
				foreach($lendrequests as $lendrequest){
					$retVal .= $lendrequest->toXML();
				}
			$retVal .= "</lendrequests>";
	
			
			$retVal .= "<reviews>";
				$reviews = $this->getReviews();
				foreach ($reviews as $review){
					$retVal .= $review->toXML();	
				}
			$retVal .= "</reviews>\n";
		}
		$retVal .= "</book>\n";
	
		
		return $retVal;
	}

}
?>
